home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / documents / application framework use next >
Encoding:
Text File  |  2000-09-28  |  5.5 KB  |  236 lines

  1. APPLICATION FRAMEWORK USE                                                                  PAGE 
  2.  
  3. C++ Application Framework Use
  4. Kent Sandvik/DTS
  5.  
  6. A Pattern approach into how to use the Application framework.
  7.  
  8. Kent Sandvik/DTS
  9.  
  10.  
  11. INTRODUCTION 
  12. This document decribes case studies how to use the DTS supplied simple C++ shell for quickly writing applications. 
  13.  
  14.  
  15. CASE 1 - START AN APPLICATION AND LET IT RUN
  16. This is the case where all the end user wants is to kick alive a simple application with a window that shows something  by default:
  17.  
  18.  
  19. #ifndef _APPLICATION_
  20. #include "Application.h"
  21. #endif
  22.  
  23.  
  24. // M A I N    F U N C T I O N
  25. void main(void)
  26. {
  27.     TMyApplication * myApp = new TMyApplication;
  28.     myApp->Start();
  29. }
  30.  
  31.  
  32.  
  33. Now, this does not do much. We want to add in behavior -- in other words our own code -- in order to achieve something using the framework. However this example shows how simple it is to create an instance of a TApplication class. This will also open up a default window with nothing inside.
  34.  
  35.  
  36. CASE 2 - WRITE QUICKDRAW STATEMENTS INSIDE WINDOW
  37. Now we want to draw something inside he default window:
  38.  
  39.  
  40. #ifndef _APPLICATION_
  41. #include "Application.h"
  42. #endif
  43.  
  44. const short H = 10;
  45. const short V = 20;
  46. const short DELTA = 12;
  47.  
  48. // This is a simple test, draw something in the default window port.
  49. class TMyApplication : public TGUIApplication    
  50. {
  51. public:
  52.     virtual void Draw();
  53. };
  54.  
  55.  
  56.  
  57.  
  58. #pragma segment Application
  59. void TMyApplication::Draw()
  60. {
  61.     // Draw something into the default window.
  62.  
  63.     PenNormal();    // Set font and font size
  64.     TextFont(geneva);
  65.     TextSize(9);
  66.     TextFace(bold);
  67.  
  68.     MoveTo(H, V);
  69.     DrawString("\pTEST1: If you want to terminate");
  70.     MoveTo(H, V + DELTA);
  71.     DrawString("\pthe program you need to click");
  72.     MoveTo(H, V + 2 * DELTA);
  73.     DrawString("\pin the window close box!");
  74.  
  75.     PenNormal();    // restore pen
  76. }
  77.  
  78. // M A I N    F U N C T I O N
  79. void main(void)
  80. {
  81.     TMyApplication * myApp = new TMyApplication;
  82.     myApp->Start();
  83. }
  84.  
  85.   
  86.  
  87. As seen this is done by overriding TApplication.Draw so we could define what is happening inside the Draw member function. Note that the member function will draw when we switch between contexts, as well as when we move windows over the drawing area.
  88.  
  89.  
  90. CASE 3 - DEFINE OUR OWN WINDOW HANDLING
  91. In this case we want to control how the window is behaving, and we also want to control how the framework creates windows or documents (we assume that the earlier header information is used):
  92.  
  93.  
  94. // In this test we will override the CreateNewDocument class and use our // own special TWindow.
  95.  
  96.  
  97. class TMyApplication : public TGUIApplication
  98. {
  99. public:
  100.     virtual void DoCreateDocument();
  101. };
  102.  
  103.  
  104. class TMyWindow : public TWindow
  105. {
  106. public:
  107.     virtual void Draw();
  108. };
  109.  
  110.  
  111. void TMyApplication::DoCreateDocument()
  112. {
  113.     // Create this time my TMyWindow document, and add it to the TApplication list.
  114.     TMyWindow * aWindow = new TMyWindow;
  115.     this->AddDocument(aWindow);
  116. }
  117.  
  118.  
  119. void TMyWindow::Draw()
  120. {
  121.     // Draw something into my special window.
  122.  
  123.     PenNormal();    // Set font and font size
  124.     TextFont(geneva);
  125.     TextSize(9);
  126.     TextFace(bold);
  127.  
  128.     MoveTo(H, V);
  129.     DrawString("\pTEST2: If you want to terminate");
  130.     MoveTo(H, V + DELTA);
  131.     DrawString("\pthe program you need to click");
  132.     MoveTo(H, V + 2 * DELTA);
  133.     DrawString("\pin the window close box!");
  134.  
  135.     PenNormal();    // restore pen
  136. }
  137.  
  138.  
  139. // M A I N   F U N C T I O N
  140. void main(void)
  141. {
  142.     TMyApplication * myApp = new TMyApplication;
  143.     myApp->Start();
  144. }
  145.  
  146.  
  147.  
  148. The things to note are:
  149. • We override TApplication.DoCreateDocument so we are able to control that the new TMyWindow class is created and added to the framework that keeps track of windows.
  150. •We create a new TWindow and override TWindow.Draw with our own drawing information.
  151.  
  152.  
  153. CASE 4- GRAPHICSENVIRONMENTS
  154. The next components are used for defining the printing environment, color, pen size and fonts. They could be used to pre-define styles, and apply them when using QuickDraw. Here’s an example concerning color:
  155.  
  156.  
  157. TColorEnvironment myMetalGray(36000,
  158.                     40500,
  159.                     37500);
  160. TColorEnvironment myMetalBlue(26312,
  161.                     14340,
  162.                     47359);
  163.  
  164. #pragma segment Window
  165. void TMyWindow::Draw()
  166. {
  167.     // Paint background in metal color
  168.     myMetalGray.SetForeground();     // set gray foreground
  169.     Rect windowRect = this->GetExtent();        
  170.     // get window extent
  171.  
  172.     ::PaintRect(&windowRect);    // paint background with gray
  173.  
  174.     // Set real background & foreground colors
  175.     myMetalBlue.SetForeground(); 
  176.     // set blue paint foreground
  177.     myMetalGray.SetBackground(); 
  178.     // set gray background
  179.  
  180. If you want to go back to the original values (black&white), calle the Reset member function.
  181.  
  182. Here’s an example where Fontenvironments are used:
  183.  
  184. TFontEnvironment myGenevaBold(srcOr,
  185.                     geneva,
  186.                     9,
  187.                     bold);
  188. TFontEnvironment myGenevaInvBold(notSrcCopy,
  189.                         geneva,
  190.                         10,
  191.                         bold);
  192.  
  193. #pragma segment Window
  194. void TMyWindow::Draw()
  195. {
  196.     // Draw something into my special window.
  197.     ::MoveTo(H, V);
  198.     myGenevaBold.Set();
  199.     ::DrawString("\pThis is a color and Quickdraw test.");
  200.  
  201.     ::MoveTo(H, V + DELTA);
  202.     myGenevaInvBold.Set();
  203.     ::DrawString("\pI will use various classes, do");
  204.  
  205.     ::MoveTo(H, V + 2 * DELTA);
  206.     myGenevaBold.Reset();    // reset font values to default state
  207.     ::DrawString("\pthey they work or not?");
  208. }
  209.  
  210.  
  211.  
  212. Finally we have a case where a pen line environment is defined and used:
  213.  
  214. TPenEnvironment myThickLine(srcOr,
  215.                     qd. dkGray,
  216.                     5,
  217.                     5);
  218.  
  219. #pragma segment Window
  220. void TMyWindow::Draw()
  221. {
  222.  
  223.     ::MoveTo(H, V + 3 * DELTA);
  224.     myThickLine.Set();    // draw thick line
  225.     ::Line(200, 0);
  226.     myThickLine.Reset();    // back to thin lines
  227.     ::MoveTo(H, V + 4 * DELTA);
  228.     ::Line(200, 0);
  229.     ::MoveTo(H, V + 5 * DELTA);
  230.     ::Line(200, 0);
  231.  
  232.     ::PenNormal();        // restore pen
  233. }
  234.  
  235.  
  236.